package com.kehutong.repair.app;

import java.util.Arrays;
import java.util.List;

import com.kehutong.common.util.Token;
import com.kehutong.repair.entity.*;
import com.kehutong.repair.service.admin.AdminRepairsService;
import com.kehutong.repair.service.app.AppRepairsService;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;
import com.kehutong.repair.enums.RepairsStatus;

/**
 * 投诉报修
 */
@MvcAction
@ReqMapping("/kht-bin/repair")
public class RepairAction {

    @Inject
    private AppRepairsService acceptRepairs;

    @Inject
    private AdminRepairsService adminRepairsService;

    @Inject
    private JdbcSession jdbcSession;

    @ReqMapping("/count")
    public Object count(Token token, JSONObject jsonObject) {
        return Result.success(acceptRepairs.count(token, jsonObject));
    }

    @ReqMapping("/countMore")
    public Object countMore(Token token, JSONObject jsonObject) {
        return Result.success(acceptRepairs.countMore(token, jsonObject));
    }

    @ReqMapping("/enums")
    public Object loadEnums() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("repairsStatusList", Arrays.asList(RepairsStatus.values()));
        return Result.success(jsonObject);
    }

    @ReqMapping("/list")
    public Object list(Token token, JSONObject jsonObject) {
        return Result.success(acceptRepairs.list(token, jsonObject));
    }

    @ReqMapping("/get")
    public Object get(Token token, String id) throws Exception {
        Repairs repairs = adminRepairsService.get(token, id);
        if (Objects.isNull(repairs)) {
            return Result.error(0, "对象不存在");
        }
        return Result.success(repairs);
    }

    @ReqMapping("/accept")
    public Object acceptRepairs(Token token, String id) throws Exception {
        return Result.success(acceptRepairs.acceptRepairs(token, id));
    }

    @ReqMapping("/finish")
    public Object finish(Token token, JSONObject jsonObject) throws Exception {
        jsonObject.put("userId", token.getUuid());
        return Result.success(adminRepairsService.finish(token, jsonObject));
    }

    @ReqMapping("/change")
    public Object change(Token token, JSONObject jsonObject) throws Exception {
        return acceptRepairs.change(token, jsonObject);
    }

    @ReqMapping("/remark")
    public Object remark(Token token, String remark, String id) throws Exception {
        return acceptRepairs.remark(token, remark, id);
    }

    @ReqMapping("/getRemark")
    public Object getRemark(String id) throws Exception {
        return jdbcSession.findArray(Remark.class).eq("orderId", id)
                .order("createTime", "desc").exe();
    }

    @ReqMapping("/getRoll")
    public Object getRoll(String id) {
        List<RollOutDetail> list = jdbcSession.findArray(RollOutDetail.class)
                .eq("repairsId", id)
                .order("createTime", "desc").exe();
        return Result.success(list);
    }
}
